package cn.sherer.utils;

import java.io.File;
import java.net.URL;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;

/**
 * 文件工具类
 */
public class FileUtils {

    /**
     * 获取文件全名
     * D:\Folder\SubFolder\File.txt 》 File.txt
     * D:\Folder\SubFolder\ 》 SubFolder
     * @param path 路径
     * @return 文件全名
     */
    public static String getFileFullName(String path) {
        try {
            return new File(path).getName();
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取文件扩展名
     * D:\Folder\SubFolder\File.txt 》 .txt
     * @param path 路径
     * @return 文件扩展名
     */
    public static String getFileExtension(String path) {
        try {
            String fileName = getFileFullName(path);
            return fileName.contains(".") ? fileName.substring(fileName.lastIndexOf(".")) : "";
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取文件名
     * D:\Folder\SubFolder\File.txt 》 File
     * D:\Folder\SubFolder\ 》 SubFolder
     * @param path 路径
     * @return 文件名
     */
    public static String getFileName(String path) {
        try {
            String fileName = getFileFullName(path);
            String extension = getFileExtension(path);
            return fileName.substring(0, fileName.length() - extension.length());
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 获取文件目录
     * D:\Folder\SubFolder\File.txt 》 D:\Folder\SubFolder
     * @param path 路径
     * @return 文件目录
     */
    public static String getFileDirectoryName(String path) {
        try {
            return new File(path).getParent();
        } catch (Exception e) {
            return "";
        }
    }

    /**
     * 读取所有文本内容
     * @param path 文件路径
     * @return 文本内容
     * @throws IOException 读取文本内容异常
     */
    public static String readAllText(String path) throws IOException {
        String content = "";
        File file = new File(path);
        if (!file.exists()) { return content; }
        FileInputStream stream = new FileInputStream(file);
        InputStreamReader inputStream = new InputStreamReader(stream, "UTF-8");
        BufferedReader reader = new BufferedReader(inputStream);
        String line = null;
        while ((line = reader.readLine()) != null) { content += line; }
        reader.close();
        inputStream.close();
        stream.close();
        return content;
    }

    /**
     * 创建文件
     * @param url 网络路径
     * @param path 文件路径
     * @return 文件
     * @throws IOException 文件创建异常
     * @throws MalformedURLException 网络路径异常
     */
    public static File createFile(String url, String path) throws MalformedURLException, IOException {
        return createFile(url, new File(path));
    }

    /**
     * 创建文件
     * @param url 网络路径
     * @param file 文件
     * @return 文件
     * @throws IOException 文件创建异常
     * @throws MalformedURLException 网络路径异常
     */
    public static File createFile(String url, File file) throws MalformedURLException, IOException {
        HttpURLConnection httpUrl = (HttpURLConnection) new URL(url).openConnection();
        httpUrl.connect();
        InputStream ins = httpUrl.getInputStream();
        if (file.exists()) { file.delete(); }
        OutputStream os = new FileOutputStream(file);
        int bytesRead;
        int len = 8192;
        byte[] buffer = new byte[len];
        while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
            os.write(buffer, 0, bytesRead);
        }
        os.close();
        ins.close();
        return file;
    }

    /**
     * 创建文件
     * @param bytes 字节
     * @param path 文件路径
     * @return 文件
     * @throws IOException 文件创建异常
     */
    public static File createFile(byte[] bytes, String path) throws IOException {
        return createFile(bytes, new File(path));
    }

    /**
     * 创建文件
     * @param bytes 字节
     * @param file 文件
     * @return 文件
     * @throws IOException 文件创建异常
     */
    public static File createFile(byte[] bytes, File file) throws IOException {
        FileOutputStream os = new FileOutputStream(file);
        os.write(bytes);
        os.close();
        return file;
    }

}
